package com.qiusuo0226.core.controller;

import com.qiusuo0226.mvc.util.JsonResultModel;
import com.qiusuo0226.core.pojo.dto.DictionaryAddDTO;
import com.qiusuo0226.core.service.DictionaryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * 字典项前端控制层
 * @author qiusuo
 * @date 2023/7/23 21:30
 */
@Slf4j
@CrossOrigin
@Validated
@RestController
@RequestMapping("/interface/dictionary")
public class DictionaryController {

    @Autowired
    private DictionaryService dictionaryService;

    /**
     * 字典项新增
     * @param dto
     * @return
     */
    @PostMapping("/add")
    public JsonResultModel add(@RequestBody @Valid DictionaryAddDTO dto) {
        return dictionaryService.add(dto);
    }

    /**
     * 根据字典组的编码获取该字典组下的所有字典值列表
     * @param groupCode 字典组编码
     * @param searchWord 模糊字典名
     * @return
     */
    @GetMapping("/list")
    public JsonResultModel list(@NotBlank(message = "字典组编码不能为空") String groupCode,
                                String searchWord) {
        return dictionaryService.list(groupCode, searchWord);
    }

    /**
     * 获取字典组列表
     * @param searchWord 模糊字典组名
     * @param pageNo 当前页
     * @param pageSize 当前页大小
     * @return 根据字典组名模糊搜索到的字典组列表
     */
    @GetMapping("/groupList")
    public JsonResultModel groupList(String searchWord,
                                     @RequestParam(defaultValue = "0") Integer pageNo,
                                     @RequestParam(defaultValue = "10") Integer pageSize) {
        return dictionaryService.groupList(searchWord, pageNo, pageSize);
    }

    /**
     * 字典组编辑
     * @param groupName 字典组名称
     * @param groupCode 字典组编码
     * @return
     */
    @PostMapping("/editGroup")
    public JsonResultModel editGroup(@NotBlank(message = "字典组名称不能为空") String groupName,
                                     @NotBlank(message = "字典组编码不能为空") String groupCode) {
        return dictionaryService.editGroup(groupName, groupCode);
    }

    /**
     * 字典值编辑
     * @param iid 字典值唯一标识
     * @param dictionaryName 字典值名
     * @param dictionaryCode 字典值编码
     * @param sort 排序
     * @return
     */
    @PostMapping("/edit")
    public JsonResultModel edit(@NotBlank(message = "字典值唯一标识不能为空") String iid,
                                @NotBlank(message = "字典值名不能为空") String dictionaryName,
                                @NotBlank(message = "字典值编码不能为空")String dictionaryCode,
                                @NotNull(message = "排序不能为空") Integer sort) {
        return dictionaryService.edit(iid, dictionaryName, dictionaryCode, sort);
    }

    /**
     * 字典组删除
     * @param groupCode 字典组编码
     * @return
     */
    @PostMapping("/removeGroup")
    public JsonResultModel removeGroup(@NotBlank(message = "字典组编码不能为空") String groupCode) {
        return dictionaryService.removeGroup(groupCode);
    }

    /**
     * 删除字典值
     * @param groupCode 字典组编码
     * @param dictionaryCodes 字典值编码拼接字符串，多个编码以英文逗号分隔
     * @return
     */
    @PostMapping("/remove")
    public JsonResultModel remove(@NotBlank(message = "字典组编码不能为空") String groupCode,
                                   @NotBlank(message = "字典值编码不能为空，多个编码以英文逗号分隔") String dictionaryCodes) {
        return dictionaryService.remove(groupCode, dictionaryCodes);
    }


}
